package com.dglg.spring.controller;

import com.dglg.spring.model.*;
import com.dglg.spring.service.AttendanceService;
import com.dglg.spring.service.TrainingClassService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    public AttendanceService attendanceService;

    @Autowired
    public TrainingClassService trainingClassService;

    // 根据实训班和学号获取考勤信息
    @GetMapping("/getAttendance/{trainingClassName}/{studentId}")
    public Result getAttendanceByParams(@PathVariable("trainingClassName") String trainingClassName, @PathVariable("studentId") String studentId){
        // 用 Attendance 类型的列表接收考勤信息
        List<Attendance> attendances = attendanceService.getAttendanceByParams(trainingClassName, studentId);
        // 若列表为空，则查询失败，反之查询成功
        Integer code = attendances != null ? Code.GET_OK : Code.GET_ERR;
        String msg = attendances != null ? "数据查询成功！" : "数据查询失败，请重试！";

        return new Result(code, attendances, msg);
    }

    // 根据学号查找学生所在的实训班信息
    @GetMapping("/getTrainClass/{studentId}")
    public Result getTrainingClass(@PathVariable("studentId") String studentId){
        List<TrainingClass> trainingClasses = trainingClassService.getTrainingClass(studentId);
        Integer code = trainingClasses != null ? Code.GET_OK : Code.GET_ERR;
        String msg = trainingClasses != null ? "数据查询成功！" : "数据查询失败，请重试！";

        return new Result(code, trainingClasses, msg);
    }

    //根据实训班名称查找教师信息
    @GetMapping("/getTeacherByClass/{className}")
    public Result getTeacherByClass(@PathVariable("className") String className){
        List<Teacher> teachers = trainingClassService.getTeacherByClass(className);
        Integer code = teachers != null ? Code.GET_OK : Code.GET_ERR;
        String msg = teachers != null ? "数据查询成功！" : "数据查询失败，请重试！";

        return new Result(code, teachers, msg);
    }

    // 根据实训班名称和学号查找项目信息
    @GetMapping("/getProject/{className}/{studentId}")
    public Result getProject(@PathVariable("className") String className, @PathVariable("studentId") String studentId){
        List<TrainingClass> projects = trainingClassService.getProject(className, studentId);
        Integer code = projects != null ? Code.GET_OK : Code.GET_ERR;
        String msg = projects != null ? "数据查询成功！" : "数据查询失败，请重试！";

        return new Result(code, projects, msg);
    }

    // 查询所有实训班
    @GetMapping("/getTrainClass")
    public Result getTrainClass(){
        List<String> classes = trainingClassService.getTrainClass();

        Integer code = classes != null ? Code.GET_OK : Code.GET_ERR;
        String msg = classes != null ? "数据查询成功！" : "数据查询失败，请重试！";

        return new Result(code, classes, msg);
    }

    // 根据实训班查询实训小组
    @GetMapping("/getGroup/{className}")
    public Result getGroupByClass(@PathVariable("className") String className){
        List<String> groups = trainingClassService.getGroupByClass(className);

        Integer code = groups != null ? Code.GET_OK : Code.GET_ERR;
        String msg = groups != null ? "数据查询成功！" : "数据查询失败，请重试！";

        return new Result(code, groups, msg);
    }

    // 根据学生姓名搜索实训班学生信息
    @GetMapping("/getTrainStudent/{studentName}")
    public Result getTrainingStudent(@PathVariable("studentName") String studentName){
        List<Student> trainingStu = trainingClassService.getTrainingStudent(studentName);
        Integer code = trainingStu.size() != 0 ? Code.GET_OK : Code.GET_ERR;
        String msg = trainingStu.size() != 0 ? "数据查询成功！" : "数据查询失败，请重试！";
        System.out.println(trainingStu);
        return new Result(code, trainingStu, msg);
    }

    // 根据实训班级名称和学号查找小组信息
    @GetMapping("/getGroupByIdClass/{studentId}/{className}")
    public Result getGroupByIdClass(@PathVariable("studentId") String studentId, @PathVariable("className") String className){
        List<TrainingStudent> trainingStudents = trainingClassService.getGroupByIdClass(studentId, className);
        Integer code = trainingStudents != null ? Code.GET_OK : Code.GET_ERR;
        String msg = trainingStudents != null ? "数据查询成功！" : "数据查询失败，请重试！";

        return new Result(code, trainingStudents, msg);
    }

    // 根据学号查找小组信息
    @GetMapping("/getGroupById/{studentId}")
    public Result getGroupById(@PathVariable("studentId") String studentId){
        List<TrainingGroup> trainingGroups = trainingClassService.getGroupById(studentId);
        Integer code = trainingGroups != null ? Code.GET_OK : Code.GET_ERR;
        String msg = trainingGroups != null ? "数据查询成功！" : "数据查询失败，请重试！";

        return new Result(code, trainingGroups, msg);
    }

    // 根据电话查询教师信息
    @GetMapping("/getTeacherByPhone/{phone}")
    public Result getTeacherByPhone(@PathVariable("phone") String phone){
        List<Teacher> teachers = trainingClassService.getTeacherByPhone(phone);
        Integer code = teachers != null ? Code.GET_OK : Code.GET_ERR;
        String msg = teachers != null ? "数据查询成功！" : "数据查询失败，请重试！";

        return new Result(code, teachers, msg);
    }

    // 查询教师信息
    @GetMapping("/getTeacher")
    public Result getTeacher(){
        List<Teacher> teachers = trainingClassService.getTeacher();
        Integer code = teachers != null ? Code.GET_OK : Code.GET_ERR;
        String msg = teachers != null ? "数据查询成功！" : "数据查询失败，请重试！";

        return new Result(code, teachers, msg);
    }

    // 查询专家信息
    @GetMapping("/getExpert")
    public Result getExpert(){
        List<Expert> experts = trainingClassService.getExpert();
        Integer code = experts != null ? Code.GET_OK : Code.GET_ERR;
        String msg = experts != null ? "数据查询成功！" : "数据查询失败，请重试！";

        return new Result(code, experts, msg);
    }

    // 查询所有小组
    @GetMapping("/getGroupName")
    public Result getGroup(){
        List<String> groups = trainingClassService.getGroup();
        Integer code = groups != null ? Code.GET_OK : Code.GET_ERR;
        String msg = groups != null ? "数据查询成功！" : "数据查询失败，请重试！";

        return new Result(code, groups, msg);
    }

    // 查询所有实训班详细信息
    @GetMapping("/getClassAll")
    public Result getTrainingClassAll(){
        List<TrainingClass> classes = trainingClassService.getTrainingClassAll();
        Integer code = classes != null ? Code.GET_OK : Code.GET_ERR;
        String msg = classes != null ? "数据查询成功！" : "数据查询失败，请重试！";

        return new Result(code, classes, msg);
    }

    // 根据日期获取日志信息(获取某一天的日志信息)
    @GetMapping("/getOneLogDate/{logDate}")
    public Result getOneByLogDate(@PathVariable("logDate") String logDate){
        List<StudentLog> logs = trainingClassService.getOneByLogDate(logDate);
        Integer code = logs != null ? Code.GET_OK : Code.GET_ERR;
        String msg = logs != null ? "数据查询成功！" : "数据查询失败，请重试！";

        return new Result(code, logs, msg);
    }

    // 根据教师电话获取实训班学生信息
    @GetMapping("/getStudentByPhone/{phone}")
    public Result getStudentByPhone(@PathVariable("phone") String phone){
        List<TrainingStudent> trainingStudents = trainingClassService.getStudentByPhone(phone);
        Integer code = trainingStudents != null ? Code.GET_OK : Code.GET_ERR;
        String msg = trainingStudents != null ? "数据查询成功！" : "数据查询失败，请重试！";

        return new Result(code, trainingStudents, msg);
    }

    // 获取实训班小组的专家评分
    @GetMapping("/getScoreMessage")
    public Result getScoreMessage(){
        List<String> list = trainingClassService.getScoreMessage();
        System.out.println(list);
        Integer code = list != null ? Code.GET_OK : Code.GET_ERR;
        String msg = list != null ? "数据查询成功！" : "数据查询失败，请重试！";

        return new Result(code, list, msg);
    }

    // 根据教师电话和学生名字查询实训班学生信息
    @RequestMapping(value = "/queryStudentByPhoneAndName", method = RequestMethod.POST)
    public Result queryStudentByPhoneAndName(HttpServletRequest request) {
        List<TrainingStudent> trainingStudents = trainingClassService.queryStudentByPhoneAndName(request.getParameter("phone"),request.getParameter("name"));
        Integer code = trainingStudents != null ? Code.GET_OK : Code.GET_ERR;
        String msg = trainingStudents != null ? "数据查询成功！" : "数据查询失败，请重试！";
        return new Result(code, trainingStudents, msg);
    }

    // 根据教师电话和小组名字查询实训班小组信息
    @RequestMapping(value = "/queryGroupByPhoneAndName", method = RequestMethod.POST)
    public Result queryGroupByPhoneAndName(HttpServletRequest request) {
        List<TrainingGroup> groups = trainingClassService.queryGroupByPhoneAndName(request.getParameter("phone"),request.getParameter("name"));
        Integer code = groups != null ? Code.GET_OK : Code.GET_ERR;
        String msg = groups != null ? "数据查询成功！" : "数据查询失败，请重试！";
        return new Result(code, groups, msg);
    }

    @RequestMapping("/comment")
    public Map<String, String> commentTeach(@RequestParam(value="commentItem[]") String[] commentItem, String className, String studentId) {
        Map<String, String> parameter = new HashMap<>();
        for (String item : commentItem) {
            System.out.println(item);
        }
        parameter.put("msg", "ok");
        return parameter;
    }

    // 根据实训班学生信息更新该学生项目分工
    @PostMapping("/updateDivision")
    public Result insertDivision(HttpServletRequest request) {
        // 接收前端传回的表单信息
        String studentId = request.getParameter("studentId");
        String className = request.getParameter("className");
        String division = request.getParameter("division");

        // 将接收到的信息封装成 TrainingStudent 类
        TrainingStudent trainingStudent = new TrainingStudent();
        trainingStudent.setStudentId(studentId);
        trainingStudent.setClassName(className);
        trainingStudent.setDivision(division);

        // 更新小组分工内容
        int count = trainingClassService.updateDivision(trainingStudent);

        Integer code = count != 0 ? Code.GET_OK : Code.GET_ERR;
        String msg = count != 0 ? "数据插入成功！" : "数据插入失败，请重试！";

        return new Result(code, count, msg);
    }

    // 根据学生日志相关信息更新日志
    @PostMapping("/updateLog")
    public Result updateLog(HttpServletRequest request) {
        String studentId = request.getParameter("studentId");
        String className = request.getParameter("className");
        String logDate = request.getParameter("logDate");
        String manager = request.getParameter("manager");
        String location = request.getParameter("location");
        String content = request.getParameter("content");
        String advice = request.getParameter("advice");

        StudentLog studentLog = new StudentLog();

        studentLog.setStudentId(studentId);
        studentLog.setClassName(className);
        studentLog.setLogDate(logDate);
        studentLog.setManager(manager);
        studentLog.setLocation(location);
        studentLog.setContent(content);
        studentLog.setAdvice(advice);

        int count = trainingClassService.updateLog(studentLog);

        Integer code = count != 0 ? Code.GET_OK : Code.GET_ERR;
        String msg = count != 0 ? "数据更新成功！" : "数据更新失败，请重试！";

        return new Result(code, count, msg);
    }

    // 根据实训班名称和教师名字更新教师信息
    @PostMapping("/updateTeacher")
    public Result updateTeacher(@RequestBody Map<String, String> map) {
        String className = map.get("className");
        String teacherName = map.get("teacherName");
        System.out.println(className);
        System.out.println(teacherName);

        // 在教师表中更新实训班名称
        int count = trainingClassService.updateTeacher(className, teacherName);

        // 在实训班中指定教师
        count = trainingClassService.updateClass(className, teacherName);

        Integer code = count != 0 ? Code.GET_OK : Code.GET_ERR;
        String msg = count != 0 ? "数据更新成功！" : "数据更新失败，请重试！";

        return new Result(code, count, msg);
    }

    // 根据学号和实训班名称查找学生日志信息
    @GetMapping("/getStudentLog/{studentId}/{className}/{pageNum}")
    public Result getStudentLog(Model model, @PathVariable("studentId") String studentId, @PathVariable("className") String className, @PathVariable("pageNum") Integer pageNum){
        PageHelper.startPage(pageNum,10);   // 确定分页时每一页的行数
        List<StudentLog> studentLogs = trainingClassService.getStudentLog(studentId, className);
        PageInfo<StudentLog> pageInfo = new PageInfo<>(studentLogs);
        model.addAttribute("pageInfo",pageInfo);

        Integer code = pageInfo != null ? Code.GET_OK : Code.GET_ERR;
        String msg = pageInfo != null ? "数据查询成功！" : "数据查询失败，请重试！";

        return new Result(code, pageInfo, msg);
    }

    // 添加教师信息
    @RequestMapping(value = "/addTeacher", method = RequestMethod.POST)
    public Result addTeacher(@RequestBody Map<String, String> map) {

        Teacher teacher = new Teacher();
        // 将接收到的表单信息封装成 Teacher 类
        teacher.setTeacherName(map.get("teacherName"));
        teacher.setGender(map.get("gender"));
        teacher.setTitle(map.get("title"));
        teacher.setPhone(map.get("phone"));
        teacher.setEmail(map.get("email"));
        teacher.setIntroduct(map.get("introduct"));

        // 插入教师信息
        int count = trainingClassService.insertTeacher(teacher);

        Integer code = count != 0 ? Code.GET_OK : Code.GET_ERR;
        String msg = count != 0 ? "数据插入成功！" : "数据插入失败，请重试！";

        return new Result(code, count, msg);
    }

    // 添加用户账号信息
    @RequestMapping(value = "/addUser", method = RequestMethod.POST)
    public Result addUser(@RequestBody Map<String, String> map) {

        User user = new User();
        user.setAccount(map.get("account"));
        user.setPassword(map.get("password"));
        user.setFlag(Integer.parseInt(map.get("flag")));

        int count = trainingClassService.insertUser(user);

        Integer code = count != 0 ? Code.GET_OK : Code.GET_ERR;
        String msg = count != 0 ? "数据插入成功！" : "数据插入失败，请重试！";

        return new Result(code, count, msg);
    }

    // 添加实训班信息
    @RequestMapping(value = "/addClass", method = RequestMethod.POST)
    public Result addClass(@RequestBody Map<String, String> map) {

        TrainingClass trainingClass = new TrainingClass();
        trainingClass.setClassName(map.get("className"));
        trainingClass.setCompany(map.get("company"));
        trainingClass.setPeriod(map.get("period"));
        trainingClass.setGrade(map.get("grade"));
        trainingClass.setStartTime(map.get("startTime"));
        trainingClass.setEndTime(map.get("endTime"));

        int count = trainingClassService.insertClass(trainingClass);

        Integer code = count != 0 ? Code.GET_OK : Code.GET_ERR;
        String msg = count != 0 ? "数据插入成功！" : "数据插入失败，请重试！";

        return new Result(code, count, msg);
    }

    // 新增专家信息
    @RequestMapping(value = "/addExpert", method = RequestMethod.POST)
    public Result addExpert(@RequestBody Map<String, String> map) {

        Expert expert = new Expert();
        expert.setExpertName(map.get("expertName"));
        expert.setTitle(map.get("title"));
        expert.setPhone(map.get("phone"));
        expert.setGender(map.get("gender"));
        expert.setUnit(map.get("unit"));
        expert.setEmail(map.get("email"));


        int count = trainingClassService.insertExpert(expert);

        Integer code = count != 0 ? Code.GET_OK : Code.GET_ERR;
        String msg = count != 0 ? "数据插入成功！" : "数据插入失败，请重试！";

        return new Result(code, count, msg);
    }

    // 添加实训班学生
    @RequestMapping(value = "/addTrainingStu", method = RequestMethod.POST)
    public Result addTrainingStu(@RequestBody Map<String, String> map) {

        int count = trainingClassService.insertTrainingStu(map.get("className"), map.get("studentId"));

        Integer code = count != 0 ? Code.GET_OK : Code.GET_ERR;
        String msg = count != 0 ? "数据插入成功！" : "数据插入失败，请重试！";

        return new Result(code, count, msg);
    }

    // 添加实训班学生
    @RequestMapping(value = "/addClassByName", method = RequestMethod.POST)
    public Result addClassByName(@RequestBody Map<String, String> map) {

        int count = trainingClassService.insertClassByName(map.get("className"), map.get("teacher"));

        Integer code = count != 0 ? Code.GET_OK : Code.GET_ERR;
        String msg = count != 0 ? "数据插入成功！" : "数据插入失败，请重试！";

        return new Result(code, count, msg);
    }

    // 导入考勤信息
    @RequestMapping(value = "/addAttendance", method = RequestMethod.POST)
    public Result addAttendance(@RequestBody Map<String, String> map) {
        // 创建 Attendance 对象接收map表单信息
        Attendance attendance = new Attendance();
        attendance.setStudentId(map.get("studentId"));
        attendance.setTrainingClassName(map.get("trainingClassName"));
        attendance.setDate(map.get("date"));
        attendance.setMorning(map.get("morning"));
        attendance.setAfternoon(map.get("afternoon"));
        attendance.setEvening(map.get("evening"));

        int count = attendanceService.insert(attendance);

        Integer code = count != 0 ? Code.GET_OK : Code.GET_ERR;
        String msg = count != 0 ? "数据插入成功！" : "数据插入失败，请重试！";

        return new Result(code, count, msg);
    }

    // 根据学号删除学生信息
    @RequestMapping(value = "/deleteStu", method = RequestMethod.POST)
    public Result deleteStu(@RequestBody Map<String, String> map) {

        // 要删除学生相关信息，需将实训班中的学生信息一起删除
        int count = trainingClassService.deleteTrainingStu(map.get("className"), map.get("studentId"));
        count = trainingClassService.deleteStudent(map.get("studentId"));

        Integer code = count != 0 ? Code.GET_OK : Code.GET_ERR;
        String msg = count != 0 ? "数据删除成功！" : "数据删除失败，请重试！";

        return new Result(code, count, msg);
    }

    // 根据实训班名称删除实训班信息
    @RequestMapping(value = "/deleteClass", method = RequestMethod.POST)
    public Result deleteClass(@RequestBody Map<String, String> map) {

        int count = trainingClassService.deleteClass(map.get("className"));

        Integer code = count != 0 ? Code.GET_OK : Code.GET_ERR;
        String msg = count != 0 ? "数据删除成功！" : "数据删除失败，请重试！";

        return new Result(code, count, msg);
    }

    // 根据手机号删除专家信息
    @RequestMapping(value = "/deleteExpert", method = RequestMethod.POST)
    public Result deleteExpert(HttpServletRequest request) {

        int count = trainingClassService.deleteExpert(request.getParameter("phone"));

        Integer code = count != 0 ? Code.GET_OK : Code.GET_ERR;
        String msg = count != 0 ? "数据删除成功！" : "数据删除失败，请重试！";

        return new Result(code, count, msg);
    }

    // 根据实训班名称删除其教师
    @RequestMapping(value = "/deleteTeacher", method = RequestMethod.POST)
    public Result deleteTeacher(@RequestBody Map<String, String> map) {

        int count1 = trainingClassService.deleteTeacher(map.get("className"));
        int count2 = trainingClassService.deleteTeacher2(map.get("className"));

        Integer code = count2 != 0 ? Code.GET_OK : Code.GET_ERR;
        String msg = count2 != 0 ? "数据删除成功！" : "数据删除失败，请重试！";

        return new Result(code, count2, msg);
    }

    @RequestMapping(value = "/deleteTrainStu", method = RequestMethod.POST)
    public Result deleteTrainStu(@RequestBody Map<String, String> map) {

        int count = trainingClassService.deleteTrainStu(map.get("className"), map.get("studentId"));

        Integer code = count != 0 ? Code.GET_OK : Code.GET_ERR;
        String msg = count != 0 ? "数据删除成功！" : "数据删除失败，请重试！";

        return new Result(code, count, msg);
    }
}
